<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-row>
        <el-col :span="8">
          <el-form-item label="文章标题" prop="title">
            <el-input
              v-model="queryParams.title"
              placeholder="请输入文章标题"
              clearable
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="文章作者" prop="title">
            <el-input
              v-model="queryParams.author"
              placeholder="请输入文章作者"
              clearable
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="博客分类" prop="categoryId">
            <el-select v-model="queryParams.categoryId" placeholder="请选择博客分类" clearable>
              <el-option
                v-for="dict in categoryList"
                :key="dict.categoryId"
                :label="dict.categoryName"
                :value="dict.categoryId"
              />
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="8">
          <el-form-item label="发布时间">
            <el-date-picker
              v-model="daterangeCreateTime"
              style="width: 240px"
              value-format="yyyy-MM-dd"
              type="daterange"
              range-separator="-"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
            ></el-date-picker>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item>
            <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
            <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>

    <el-table v-loading="loading" :data="articleList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="文章id" align="center" prop="articleId"/>
      <el-table-column label="文章标题" align="center" prop="title"/>
      <el-table-column label="文章简介" align="center" prop="descr"/>
      <el-table-column label="文章封面" align="center" prop="cover" width="100">
        <template slot-scope="scope">
          <image-preview :src="scope.row.cover" :width="50" :height="50"/>
        </template>
      </el-table-column>
      <el-table-column label="文章标签" align="center" prop="tags">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_common_status" :value="scope.row.tags"/>
        </template>
      </el-table-column>
      <el-table-column label="发布人" align="center" prop="author"/>
      <el-table-column label="文章更新时间" align="center" prop="updateTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.updateTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="文章发布时间" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="浏览量" align="center" prop="readCount"/>
      <el-table-column label="评论数" align="center" prop="commentsCount"/>
      <el-table-column label="博客分类" align="center" prop="categoryId">
        <template slot-scope="scope">
          <el-tag effect="dark">{{ scope.row.categoryName }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="文章状态" align="center" prop="status">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.blog_article_status" :value="scope.row.status"/>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" fixed="right" width="160">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
          >审核
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            style="color: red"
            @click="handleDelete(scope.row)"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改博客文章对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="95%" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="文章标题" prop="title">
              <el-input v-model="form.title" placeholder="请输入文章标题" disabled/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="文章简介" prop="descr">
              <el-input v-model="form.descr" placeholder="请输入文章简介" disabled/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="8">
            <el-form-item label="文章标签" prop="tags" disabled>
              <el-select v-model="form.tags"
                         placeholder="请选择文章标签"
                         disabled>
                <el-option
                  v-for="dict in dict.type.sys_common_status"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="博客分类" prop="categoryId">
              <el-select v-model="form.categoryId" placeholder="请选择文章分类" disabled>
                <el-option
                  v-for="dict in categoryList"
                  :key="dict.categoryId"
                  :label="dict.categoryName"
                  :value="dict.categoryId"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-form-item label="文章封面" prop="cover">
            <image-preview :src="form.cover" :width="50" :height="50"/>
          </el-form-item>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="文章内容">
              <div class="ql-editor" v-html="form.content" style="min-height: 200px">

              </div>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="danger" @click="submitForm('3')">拒 绝</el-button>
        <el-button type="primary" @click="submitForm('4')">通 过</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {listArticle, getArticle, delArticle, addArticle, updateArticle} from "@/api/blog/article";
import {listCategory} from "@/api/blog/category";
import {getDicts} from "@/api/system/dict/data";

export default {
  name: "ArticleShow",
  dicts: ['sys_common_status', 'blog_article_status'],
  data() {
    return {
      // name: null,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 博客文章表格数据
      articleList: [],
      //博客文章分类数据
      categoryList: [],
      //博客文章状态数据
      articleStatusList: [],
      //按钮文字
      buttonName: null,
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 文章状态时间范围
      daterangeUpdateTime: [],
      // 文章状态时间范围
      daterangeCreateTime: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        title: null,
        userId: null,
        createTime: null,
        categoryId: null,
        articleStatus: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        title: [
          {required: true, message: "文章标题不能为空", trigger: "blur"}
        ],
      }
    };
  },
  created() {
    this.getCategory();
    this.getArticleStatus();
    this.getList();
  },
  methods: {
    /* 查询博客文章状态 */
    getArticleStatus() {
      let type = "blog_article_status";
      getDicts(type).then(response => {
        const role = this.$store.getters.roles[0];
        response.data.map(dict => {
          //回显不一致，数据格式问题，比对的是值
          if (dict.dictValue !== '1') {
            this.articleStatusList.push({value: dict.dictValue, label: dict.dictLabel})
          }
        });
      });
    },

    /* 查询博客文章分类列表 */
    getCategory() {
      listCategory().then(response => {
        this.categoryList = response.rows;
      })
    },
    /** 查询博客文章列表 */
    getList() {
      this.loading = true;
      this.queryParams.params = {};
      //查看自己的文章
      this.queryParams.userId = this.$store.getters.userId
      if (null != this.daterangeUpdateTime && '' != this.daterangeUpdateTime) {
        this.queryParams.params["beginUpdateTime"] = this.daterangeUpdateTime[0];
        this.queryParams.params["endUpdateTime"] = this.daterangeUpdateTime[1];
      }
      if (null != this.daterangeCreateTime && '' != this.daterangeCreateTime) {
        this.queryParams.params["beginCreateTime"] = this.daterangeCreateTime[0];
        this.queryParams.params["endCreateTime"] = this.daterangeCreateTime[1];
      }
      listArticle(this.queryParams).then(response => {
        this.articleList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        articleId: null,
        title: null,
        descr: null,
        content: null,
        cover: null,
        tags: null,
        userId: null,
        updateTime: null,
        createTime: null,
        readCount: null,
        commentsCount: null,
        categoryId: null,
        status: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.daterangeUpdateTime = [];
      this.daterangeCreateTime = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.articleId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加博客文章";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const articleId = row.articleId || this.ids
      getArticle(articleId).then(response => {
        this.form = response.data;
        this.title = "修改博客文章";
        this.open = true;
      });
    },
    /** 提交按钮 */
    submitForm(status) {
      this.form.status = status;
      if (this.form.articleId != null) {
        updateArticle(this.form).then(response => {
          this.$modal.msgSuccess("修改成功");
          this.open = false;
          this.getList();
        });
      }
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const articleIds = row.articleId || this.ids;
      this.$modal.confirm('是否确认删除博客文章编号为"' + articleIds + '"的数据项？').then(function () {
        return delArticle(articleIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('blog/article/export', {
        ...this.queryParams
      }, `article_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
